package com.bjpowernode.oa.web.action;

import com.bjpowernode.oa.bean.User;
import com.bjpowernode.oa.util.DBUtil;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@WebServlet({"/user/login","/user/exit"})
public class UserServlet extends HttpServlet {
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String servletPath = request.getServletPath();
        if("/user/login".equals(servletPath)){
            doLogin(request,response);
        }else if("/user/exit".equals(servletPath)){
            doExit(request,response);
        }
    }

    private void doExit(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException{
        //获取session对象，销毁session
        HttpSession session = request.getSession(false);
        if(session!=null){
            session.removeAttribute("user");

            //手动销毁session对象
            session.invalidate();

            //销毁cookie
            Cookie[] cookies = request.getCookies();
            if (cookies != null) {
                for (Cookie cookie : cookies) {
                    String name=cookie.getName();
                    if("username".equals(name)||"password".equals(name)){
                        //设置cookie的有效期为0，表示手动删除cookie
                        cookie.setMaxAge(0);
                        //设置cookie的路径
                        cookie.setPath(request.getContextPath());
                        //响应cookie给浏览器，浏览器会将之前的cookie覆盖
                        response.addCookie(cookie);
                    }
                }
            }
            response.sendRedirect(request.getContextPath());
        }
    }

    protected void doLogin(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out= response.getWriter();
        Connection con=null;
        PreparedStatement pstmt=null;
        ResultSet rs=null;
        int count=0;
        try {
            con= DBUtil.getConnection();
            String sql="select * from t_user where username=? and password=?";
            pstmt=con.prepareStatement(sql);
            pstmt.setString(1,username);
            pstmt.setString(2,password);
            rs=pstmt.executeQuery();
            if(rs.next()){
                count=1;
            }
        } catch (SQLException exception) {
            exception.printStackTrace();
        }finally {
            DBUtil.close(con,pstmt,rs);
        }
        if(count==1){
            HttpSession session = request.getSession();
            User user=new User(username,password);
            session.setAttribute("user",user);

            //登录成功，用户选择了十天内免登录
            String f = request.getParameter("f");
            if("1".equals(f)){
                Cookie cookie=new Cookie("username",username);
                Cookie cookie2 = new Cookie("password", password);
                cookie.setMaxAge(10*24*60*60);
                cookie2.setMaxAge(10*24*60*60);

                //设置cookie的path 只要访问这个应用 浏览器就一定要携带这两个cookie
                cookie.setPath(request.getContextPath());
                cookie2.setPath(request.getContextPath());

                response.addCookie(cookie);
                response.addCookie(cookie2);
            }

            response.sendRedirect(request.getContextPath()+"/dept/list");
        }else{
            response.sendRedirect(request.getContextPath()+"/loginerror.jsp");
        }
    }
}
